package com.feeyo.groundservice.composewidget.usage

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.SwipeToDismissBox
import androidx.compose.material3.Text
import androidx.compose.material3.rememberSwipeToDismissBoxState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.feeyo.groundservice.composewidget.usage.base.BaseComposeActivity
import com.feeyo.groundservice.composewidget.util.rel
import com.feeyo.groundservice.composewidget.util.rememberMutableStateOf
import com.feeyo.groundservice.base_util.toast
import com.feeyo.groundservice.composewidget.view.constant.ComposePosition
import com.feeyo.groundservice.composewidget.view.sidemenu.SlideMenu
import com.feeyo.groundservice.composewidget.view.touch_bar.StarBar
import com.feeyo.groundservice.composewidget.view.touch_bar.VectorStarBar
import com.feeyo.groundservice.composewidget.view.util.VerticalSpace

/**
 *作者：QLH on 2023-08-10
 *描述：
 */
class SlideMenuActivity : BaseComposeActivity() {

    private val width = 160.dp
    private val height = 60.dp

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            Column(
                modifier = Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                SystemSwipeToDismiss()
                VerticalSpace(10.dp)
                //左滑
                ComposeViewsSwipeToDismiss(true)
                VerticalSpace(10.dp)
                ComposeViewsSwipeToDismiss(false)
                VerticalSpace(10.dp)
                //右滑
                ComposeViewsSwipeToDismiss(true, ComposePosition.Start)
                VerticalSpace(10.dp)
                ComposeViewsSwipeToDismiss(false, ComposePosition.Start)
                VerticalSpace(10.dp)

                //下滑
                ComposeViewsSwipeToDismiss(true, ComposePosition.Top)
                VerticalSpace(10.dp)
                ComposeViewsSwipeToDismiss(false, ComposePosition.Top)
                VerticalSpace(10.dp)

                //上滑
                ComposeViewsSwipeToDismiss(true, ComposePosition.Bottom)
                VerticalSpace(10.dp)
                ComposeViewsSwipeToDismiss(false, ComposePosition.Bottom)
                VerticalSpace(10.dp)
                StarBarView()
                StarBarView1()


            }
        }

    }

    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    private fun SystemSwipeToDismiss() {
        val state = rememberSwipeToDismissBoxState()
        SwipeToDismissBox(
            state = state, backgroundContent = {
                Text(
                    text = "background", modifier = Modifier.fillMaxSize().background(Color.Gray)
                )
            }, modifier = Modifier.size(width, height)
        ) {
            Text(
                text = "系统自带的侧滑", modifier = Modifier.fillMaxSize().background(Color.LightGray)
            )
        }
    }

    @Composable
    private fun ComposeViewsSwipeToDismiss(
        contentIsMove: Boolean, orientation: ComposePosition = ComposePosition.End
    ) {
        val r = when (orientation) {
            ComposePosition.Start -> "右滑"
            ComposePosition.End -> "左滑"
            ComposePosition.Top -> "下滑"
            ComposePosition.Bottom -> "上滑"
        }
        val fix = if (contentIsMove) "内容移动" else "内容固定"
        SlideMenu(
            minScrollPosition = 0.dp,
            maxScrollPosition = if (orientation == ComposePosition.Start || orientation == ComposePosition.End) width / 2 else height / 2,
            backgroundContent = {
                Text(text = "删除",
                    Modifier.fillMaxSize().background(Color.Red)
                        .clickable { toast("删除") })
            },
            contentIsMove = contentIsMove,
            composePosition = orientation,
            modifier = Modifier.size(width, height)
        ) {
            Text(
                text = "$r $fix",
                modifier = Modifier.fillMaxSize().background(Color.LightGray)
            )
        }
    }

    @Composable
    fun StarBarView(){
        var star by rememberMutableStateOf(value = 0)
        VectorStarBar(
            starValue = star,
            starSize = 30.dp,
            onStarValueChange = { star = it },
            onTouchUpEvent = rel { println("star=$star") },
        )
    }

    @Composable
    fun StarBarView1(){
        var star by rememberMutableStateOf(value = 0)
        StarBar(
            starValue = star,
            starSize = 30.dp,
            onStarValueChange = { star = it },
            onTouchUpEvent = rel { println("star=$star") },
        )
    }
}